//
//  TestPopoverViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2024/11/18.
//  Copyright © 2024 moon. All rights reserved.
//

#import "TestPopoverViewController.h"
#import "MKUITestCache.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellButton+Test.h"
#import "NSDictionary+Test.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "Menu.h"

@interface TestPopover_MKUIElementViewDetail : MKUIElementViewDetail
@property(nonatomic,copy,nullable) void(^whenLongPress)(MKUIElementViewDetail *view);
@end
@implementation TestPopover_MKUIElementViewDetail
@end
@interface TestPopoverViewParam: NSObject
@property(nonatomic,assign) UIEdgeInsets contentInsets;//contentView的外边距,默认为(1,1,1,1)
@property(nonatomic,assign) UIEdgeInsets popoverMargins;//弹出框与其容器的边距，默认为(10,10,10,10)
@property(nonatomic,assign) UIPopoverArrowDirection permittedArrowDirections;
@property(nonatomic,assign) CGFloat arrowSpacing;
@property(nonatomic,assign) BOOL shouldAutoDismiss;
@property(nonatomic,assign) CGSize upArrowSize;
@property(nonatomic,assign) CGFloat cornerRadius;
@property(nonatomic,strong,nullable) UIColor *borderColor;
@property(nonatomic,strong,nullable) UIColor *fillColor;
@property(nonatomic,assign) CGFloat lineWidth;
@property(nonatomic,assign) BOOL showInWindow;
@property(nonatomic,strong) NSString *popContentString;
@property(nonatomic,assign) BOOL useDefaultParam;

- (void)setValueFromJson:(NSDictionary *)json;
- (NSDictionary *)jsonValue;
@end
@implementation TestPopoverViewParam
- (id)init{
    if(self=[super init]){
        self.contentInsets = MKUIEdgeInsetsMakeSameEdge(10);
        self.popoverMargins = MKUIEdgeInsetsMakeSameEdge(10);
        self.permittedArrowDirections = UIPopoverArrowDirectionAny;
        self.arrowSpacing = 5;
        self.shouldAutoDismiss = YES;
        self.upArrowSize = CGSizeMake(9, 7);
        self.cornerRadius = 7;
        self.borderColor = UIColor.mk_popoverBorderColor;
        self.fillColor = UIColor.mk_popoverBorderColor;
        self.lineWidth = 1;
        self.showInWindow = YES;
        self.popContentString = @"1.弹出框内容弹出框内容弹出框内容弹出框内容\n2.弹出框内容\n3.弹出框内容";
        self.useDefaultParam = NO;
    }
    return self;
}
+ (TestPopoverViewParam *)cacheParam{
    TestPopoverViewParam *param = [[TestPopoverViewParam alloc] init];
    NSDictionary *json = [MKUITestCache.sharedStorage mk_dictionaryForKeyPath:@"TestPopoverViewParam"];
    [param setValueFromJson:json];
    return param;
}
- (void)saveToCache{
    [MKUITestCache.sharedStorage setObject:self.jsonValue forKey:@"TestPopoverViewParam"];
}
- (void)setValueFromJson:(NSDictionary *)json{
    if(!json)return;
    self.contentInsets = [json test_UIEdgeInsetsForKeyPath:@"contentInsets" otherwise:self.contentInsets];
    self.popoverMargins = [json test_UIEdgeInsetsForKeyPath:@"popoverMargins" otherwise:self.popoverMargins];
    self.permittedArrowDirections = (UIPopoverArrowDirection)[json mk_integerForKeyPath:@"permittedArrowDirections" otherwise:self.permittedArrowDirections];
    self.arrowSpacing = [json mk_floatForKeyPath:@"arrowSpacing" otherwise:self.arrowSpacing];
    self.shouldAutoDismiss = [json mk_boolForKeyPath:@"shouldAutoDismiss" otherwise:self.shouldAutoDismiss];
    self.upArrowSize = [json test_CGSizeForKeyPath:@"upArrowSize" otherwise:self.upArrowSize];
    self.cornerRadius = [json mk_floatForKeyPath:@"cornerRadius" otherwise:self.cornerRadius];
    self.borderColor = [json test_UIColorForKeyPath:@"borderColor" otherwise:self.borderColor];
    self.fillColor = [json test_UIColorForKeyPath:@"fillColor" otherwise:self.fillColor];
    self.lineWidth = [json mk_CGFloatForKeyPath:@"lineWidth" otherwise:self.lineWidth];
    self.showInWindow = [json mk_boolForKeyPath:@"showInWindow" otherwise:self.showInWindow];
    self.popContentString = [json mk_stringForKeyPath:@"popContentString" otherwise:self.popContentString];
    self.useDefaultParam = [json mk_boolForKeyPath:@"useDefaultParam" otherwise:self.useDefaultParam];
    
}
- (NSDictionary *)jsonValue{
    return @{
        @"contentInsets":[NSDictionary test_UIEdgeInsets:self.contentInsets],
        @"popoverMargins":[NSDictionary test_UIEdgeInsets:self.popoverMargins],
        @"permittedArrowDirections":@(self.permittedArrowDirections),
        @"arrowSpacing":@(self.arrowSpacing),
        @"shouldAutoDismiss":@(self.shouldAutoDismiss),
        @"upArrowSize":[NSDictionary test_CGSize:self.upArrowSize],
        @"cornerRadius":@(self.cornerRadius),
        @"borderColor":self.borderColor.mk_rgbaString?:@"",
        @"fillColor":self.fillColor.mk_rgbaString?:@"",
        @"lineWidth":@(self.lineWidth),
        @"showInWindow":@(self.showInWindow),
        @"popContentString":self.popContentString,
        @"useDefaultParam":@(self.useDefaultParam),
    };
}

@end

@interface TestPopoverViewController ()
@property(nonatomic,strong) UIView *anchorView;
@property(nonatomic,strong) MKUIPopoverView *anchorPopView;
@property(nonatomic,strong) UILabel *popContentView;
@property(nonatomic,strong) TestPopoverViewParam *param;
@end

@implementation TestPopoverViewController

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"Popover" viewControllerClass:self];
}
- (void)_contentViewDidTap:(id)sender{
    MKUIPopoverView *popView = [self.popContentView mk_firstSuperViewWithClass:MKUIPopoverView.class];
    [popView dismissPopoverAnimated:YES completion:nil];
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.param = [TestPopoverViewParam cacheParam];
    //
    
    UILabel *contentView = [[UILabel alloc] init];
    contentView.numberOfLines = 0;
    contentView.userInteractionEnabled = YES;
    contentView.text = self.param.popContentString;
    contentView.textColor = UIColor.systemBlueColor;
    [contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_contentViewDidTap:)]];
    self.popContentView = contentView;
    //
    self.anchorView = [[UIView alloc] init];
    self.anchorView.backgroundColor = UIColor.systemBlueColor;
    self.anchorView.layer.borderColor = UIColor.blackColor.CGColor;
    self.anchorView.layer.borderWidth = 1;
    self.anchorView.frame = CGRectMake(0, 0, 80, 80);
    [self _updateAnchorViewTrans];
    [self.anchorView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__on_anchorView_tap:)]];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
    @MK_WEAKIFY(self);
    pan.mk_moveAction.whenAction = ^(MKUIGestureRecognizerMoveAction * _Nonnull action) {
        @MK_NORMALIZE(self);
        CGPoint point = [(NSValue *)action.userInfo CGPointValue];
        CGVector dis =  action.moveDistance;
        point.x += dis.dx;
        point.y += dis.dy;
        [MKUITestCache.sharedStorage setObject:[NSValue valueWithCGPoint:point] forKey:@"TestPopoverViewController.anchorView.trans"];
        [self _updateAnchorViewTrans];
        if(action.gestureRecognizer.state==UIGestureRecognizerStateEnded||action.gestureRecognizer.state==UIGestureRecognizerStateCancelled){
            action.userInfo = [MKUITestCache.sharedStorage mk_NSValueForKeyPath:@"TestPopoverViewController.anchorView.trans" otherwise:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
        }
    };
    pan.mk_moveAction.userInfo = [MKUITestCache.sharedStorage mk_NSValueForKeyPath:@"TestPopoverViewController.anchorView.trans" otherwise:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
    [self.anchorView addGestureRecognizer:pan];
    [self.view addSubview:self.anchorView];
    //
    MKUIFlowLayoutButton *navRightButton = [[MKUIFlowLayoutButton alloc] init];
    [navRightButton setTitleColor:UIColor.systemBlueColor forState:(UIControlStateNormal)];
    [navRightButton setTitle:@"..." forState:(UIControlStateNormal)];
    [navRightButton mk_addClickActionBlock:^(TestPopoverViewController *self) {
        MKUIPopoverMenuListView *popView = [[MKUIPopoverMenuListView alloc] init];
        [popView addMenu:[MKUIPopoverMenu menuWithTitle:@"重置位置" whenClick:^(MKUIPopoverMenu * _Nonnull menu) {
            [MKUITestCache.sharedStorage setObject:[NSValue valueWithCGPoint:CGPointMake(0, 0)] forKey:@"TestPopoverViewController.anchorView.trans"];
            pan.mk_moveAction.userInfo = [MKUITestCache.sharedStorage mk_NSValueForKeyPath:@"TestPopoverViewController.anchorView.trans" otherwise:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];
            [self _updateAnchorViewTrans];
        }]];
        [popView addMenu:[MKUIPopoverMenu menuWithTitle:@"重置参数" whenClick:^(MKUIPopoverMenu * _Nonnull menu) {
            self.param = [[TestPopoverViewParam alloc] init];
            [self.param saveToCache];
            [self _reloadData];
        }]];
        popView.scrollDirection = MKUIListViewScrollDirectionVertical;
        [popView presentPopoverInWindowPointToView:navRightButton animated:YES completion:nil];
    } context:self];
    [navRightButton sizeToFit];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:navRightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self _reloadData];
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    CGRect bounds = self.safeBounds;
    CGRect f1 = bounds;
    f1.size = self.anchorView.bounds.size;
    MKCGRectAlignMidXToRect(&f1, bounds);
    MKCGRectAlignMidYToRect(&f1, bounds);
    self.anchorView.mk_frameSafety = f1;
}
- (void)_refreshParam{
    [self.param saveToCache];
    [self _configPopView];
    if(self.anchorPopView.popoverVisible){
        [self.anchorPopView reloadWithAnimated:NO completion:nil];
    }
}
- (void)_configPopView{
    if(!self.anchorPopView) return;
    if(self.param.useDefaultParam){
        if(self.anchorPopView){
            [self.anchorPopView dismissPopoverAnimated:YES completion:nil];
        }
        self.anchorPopView = [[MKUIPopoverView alloc] init];
    }else{
        self.anchorPopView.contentInsets = self.param.contentInsets;
        self.anchorPopView.popoverMargins = self.param.popoverMargins;
        self.anchorPopView.permittedArrowDirections = self.param.permittedArrowDirections;
        self.anchorPopView.arrowHeight = self.param.upArrowSize.height;
        self.anchorPopView.shouldAutoDismiss = self.param.shouldAutoDismiss;
        self.anchorPopView.arrowSpacing = self.param.arrowSpacing;
        self.anchorPopView.borderView.upArrowSize = self.param.upArrowSize;
        self.anchorPopView.borderView.cornerRadius = self.param.cornerRadius;
        self.anchorPopView.borderView.borderColor = self.param.borderColor;
        self.anchorPopView.borderView.fillColor = self.param.fillColor;
        self.anchorPopView.borderView.lineWidth = self.param.lineWidth;
    }
    self.anchorPopView.contentView = self.popContentView;
    self.anchorPopView.passthroughViews = @[self.anchorView];
    UIView *container = self.param.showInWindow?[self.anchorView mk_firstSuperViewWithClass:UIWindow.class]:self.anchorView.superview;
    self.anchorPopView.arrowPointToRect = [self.anchorView.superview convertRect:CGRectApplyAffineTransform(self.anchorView.mk_frameSafety, self.anchorView.transform) toView:container];
    self.popContentView.text = self.param.popContentString;
}
- (void)_reloadData{
    [self.listView deleteAllSections];
    [self.listView addSection];
    @MK_WEAKIFY(self);
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置contentInsets" selected:self.param.contentInsets whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            self.param.contentInsets = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置popoverMargins" selected:self.param.popoverMargins whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            self.param.popoverMargins = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementUIPopoverArrowDirection:@"设置permittedArrowDirections" selected:self.param.permittedArrowDirections whenDone:^(UIPopoverArrowDirection selectedValue) {
            @MK_NORMALIZE(self);
            self.param.permittedArrowDirections = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置upArrowSize.width" selected:self.param.upArrowSize.width whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.param.upArrowSize = CGSizeMake(selectedValue, self.param.upArrowSize.height);
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置upArrowSize.height" selected:self.param.upArrowSize.height whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.param.upArrowSize = CGSizeMake(self.param.upArrowSize.width,selectedValue);
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置shouldAutoDismiss" on:self.param.shouldAutoDismiss whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.param.shouldAutoDismiss = on;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置arrowSpacing" selected:self.param.arrowSpacing whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.param.arrowSpacing = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置cornerRadius" selected:self.param.cornerRadius whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.param.cornerRadius = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置borderColor" selected:self.param.borderColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.param.borderColor = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置fillColor" selected:self.param.fillColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.param.fillColor = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置lineWidth" selected:self.param.lineWidth whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.param.lineWidth = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置showInWindow" on:self.param.showInWindow whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.param.showInWindow = on;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置useDefaultParam" on:self.param.useDefaultParam whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.param.useDefaultParam = on;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSStringElementWithTitle:@"设置popContentString" selected:self.param.popContentString whenDone:^(NSString * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.param.popContentString = selectedValue;
            [self _refreshParam];
        }];
        [self.listView addItem:el];
    }
    [self.listView addSection];
    
    {
        TestPopover_MKUIElementViewDetail *el = [[TestPopover_MKUIElementViewDetail alloc] init];
        el.detailText = @"长按我显示弹出窗UICollectionView";
//        UITapGestureRecognizer *longGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_onLongForCell:)];
//        [el.detailLabel addGestureRecognizer:longGes];//该手势不会被触发，因为事件被scrollview拦截了
        el.whenLongPress = ^(MKUIElementViewDetail *view) {
            @MK_NORMALIZE(self);
            [self _showPopViewFromCell:view.detailLabel];
        };
        [self.listView addItem:el];
    }
    {
        TestPopover_MKUIElementViewDetail *el = [[TestPopover_MKUIElementViewDetail alloc] init];
        el.detailText = @"长按我显示弹出窗MKUIListView(横向）";
        el.whenLongPress = ^(MKUIElementViewDetail *view) {
            @MK_NORMALIZE(self);
            [self _showPopView_ListView_FromCell:view.detailLabel direction:(MKUIListViewScrollDirectionHorizontal)];
        };
        [self.listView addItem:el];
    }
    {
        TestPopover_MKUIElementViewDetail *el = [[TestPopover_MKUIElementViewDetail alloc] init];
        el.detailText = @"长按我显示弹出窗MKUIListView（竖向）";
        el.whenLongPress = ^(MKUIElementViewDetail *view) {
            @MK_NORMALIZE(self);
            [self _showPopView_ListView_FromCell:view.detailLabel direction:(MKUIListViewScrollDirectionVertical)];
        };
        [self.listView addItem:el];
    }
    //直接在cell上添加长按手势，会被scrollview拦截
    UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_onLong:)];
    [self.listView addGestureRecognizer:longGes];
    [self.listView.panGestureRecognizer addTarget:self action:@selector(_onListViewTouch:)];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.listView mk_dismissUIWindowPopoverAnimated:animated completion:nil];
}
- (void)_onListViewTouch:(UITapGestureRecognizer *)ges{
    [self.listView mk_dismissUIWindowPopoverAnimated:YES completion:nil];
}
- (void)_onLongForCell:(UILongPressGestureRecognizer *)ges{
    if(ges.state!=UIGestureRecognizerStateBegan) return;
    [self _showPopViewFromCell:ges.view];
}
- (void)_onLong:(UILongPressGestureRecognizer *)ges{
    if(ges.state!=UIGestureRecognizerStateBegan) return;
    UIView *cell = [self.listView viewAtPoint:[ges locationInView:self.listView]];
    if(![cell isKindOfClass:TestPopover_MKUIElementViewDetail.class])return;
    TestPopover_MKUIElementViewDetail *el = (TestPopover_MKUIElementViewDetail *)cell;
    if(el.whenLongPress){
        el.whenLongPress(el);
    }
    
}
- (void)_showPopViewFromCell:(UIView *)view{
    if(!view)return;
    MKUIPopoverMenuCollectionView *popView = [[MKUIPopoverMenuCollectionView alloc] init];
    for(Menu *m in [Menu sharedMenusWithCount:10]){
        [popView addMenu:[MKUIPopoverMenu menuWithTitle:m.title image:m.icon darkImage:nil whenClick:^(MKUIPopoverMenu * _Nonnull action) {
            NSLog(@"点击了按钮:%@",action.title);
        }]];
    }
    popView.passthroughViews = @[self.listView];
    [popView presentPopoverInWindowPointToView:view animated:YES completion:nil];
}
- (void)_showPopView_ListView_FromCell:(UIView *)view direction:(MKUIListViewScrollDirection)direction{
    if(!view)return;
    MKUIPopoverMenuListView *popView = [[MKUIPopoverMenuListView alloc] init];
    popView.scrollDirection = direction;
    BOOL needImage = direction==MKUIListViewScrollDirectionVertical;
    for(Menu *m in [Menu sharedMenusWithCount:10]){
        [popView addMenu:[MKUIPopoverMenu menuWithTitle:m.title image:needImage?m.icon:nil darkImage:nil whenClick:^(MKUIPopoverMenu * _Nonnull action) {
            NSLog(@"点击了按钮:%@",action.title);
        }]];
    }
    [popView presentPopoverInWindowPointToView:view animated:YES completion:nil];
}
- (void)_updateAnchorViewTrans{
    CGPoint tran = [MKUITestCache.sharedStorage mk_NSValueForKeyPath:@"TestPopoverViewController.anchorView.trans" otherwise:[NSValue valueWithCGPoint:CGPointMake(0, 0)]].CGPointValue;
    CGAffineTransform m = CGAffineTransformMakeTranslation(tran.x, tran.y);
    self.anchorView.transform = m;
    if(self.anchorPopView.popoverVisible){
        self.anchorPopView.arrowPointToRect = CGRectApplyAffineTransform(self.anchorView.mk_frameSafety, self.anchorView.transform);
        [self.anchorPopView reloadWithAnimated:NO completion:nil];
    }
}
- (void)__on_anchorView_tap:(UITapGestureRecognizer *)ges{
    if(self.anchorPopView.popoverVisible){
        @MK_WEAKIFY(self);
        [self.anchorPopView dismissPopoverAnimated:YES completion:^(BOOL finished) {
            @MK_NORMALIZE(self);
            self.anchorPopView = nil;
        }];
        return;
    }
    self.anchorPopView = [[MKUIPopoverView alloc] init];
    [self _configPopView];
    UIView *containerView = self.param.showInWindow?[self.anchorView mk_firstSuperViewWithClass:UIWindow.class]:self.anchorView.superview;
    [self.anchorPopView presentPopoverPointToView:self.anchorView inContainerView:containerView animated:YES completion:nil];
}
- (void)dealloc{
    [self.anchorPopView dismissPopoverAnimated:YES completion:nil];
}
@end
